from crawler_core.cache.download_cache import DownloadCache
from crawler_core.constant.constants import Constants
from crawler_core.constant.constants_account import ConstantAccount
from crawler_core.constant.constants_api import ConstantAPI
from crawler_core.proxy.download_proxy import DownloadProxy
from crawler_core.utils.redis_utils import RedisUtils
from lib.sys.logger_factory import LoggerFactory


class TaskDownload(object):
    logger = LoggerFactory.getLogger("TaskDownload", "download")

    def __init__(self):
        pass

    def start(self):
        # 获取任务Id
        task_id = RedisUtils.getDownloadTask()
        if task_id:
            # 获取任务--执行对象
            TaskDownload.logger.info("获取下载任务 -- [%s] --" % task_id)
            context = RedisUtils.getEventTask(task_id)
        else:
            return
        if context:
            TaskDownload.logger.info("下载任务 -- [%s] --， 获取任务内容 ..." % task_id)
            _type = context[Constants.CONTEXT_TYPE]
            corp_code = context[Constants.CONTEXT_CORP_CODE]
            username = context[Constants.CONTEXT_USERNAME]
            # 获取cookie
            cookies = RedisUtils.getCookies(_type, corp_code, username)
        else:
            return

        if cookies:
            # 开始执行任务
            TaskDownload.logger.info("下载任务 -- [%s] -- 获取 Cookies，开始执行下载..." % task_id)
            data = DownloadProxy().download(context, cookies)
            code = data[Constants.DOWNLOAD_CODE]
            # 1、解除登录锁定
            if code == ConstantAPI.RETURN_ERROR_COOKIES:
                json_data = RedisUtils.getEventTask(task_id)
                corp_code = json_data.get(Constants.CONTEXT_CORP_CODE)
                _type = json_data.get(Constants.CONTEXT_TYPE)
                username = json_data.get(Constants.CONTEXT_USERNAME)
                RedisUtils.addEventAccount(corp_code, _type, username, ConstantAccount.STATUS_COOKIES_LOSE)
                TaskDownload.logger.info("下载任务 -- [%s] -- 执行下载，下载任务失败--Cookies失效，重新登录 ..." % task_id)
                RedisUtils.setLoginTask(task_id)

            elif code == ConstantAPI.RETURN_ERROR_UNKNOWN:
                TaskDownload.logger.info("下载任务 -- [%s] -- 执行下载，下载任务失败， 任务结束 ..." % task_id)
                json_data = RedisUtils.getEventTask(task_id)
                tenant_id = json_data[Constants.API_PARAM_TENANT_ID]
                RedisUtils.delEventTask(task_id)
                RedisUtils.removeEventDownloadLock(tenant_id)
            else:
                TaskDownload.logger.info("下载任务 -- [%s] -- 执行下载，下载成功..." % task_id)
                TaskDownload.logger.info("下载任务 -- [%s] -- 执行下载，发送下载数据到缓冲池，解析数据 ..." % task_id)
                DownloadCache.add(task_id, _type, data[Constants.CACHE_DOWNLOAD_DATA])
        else:
            TaskDownload.logger.info("下载任务 -- [%s] -- 执行下载，cookies不存在，任务结束 ..." % task_id)
            json_data = RedisUtils.getEventTask(task_id)
            tenant_id = json_data[Constants.API_PARAM_TENANT_ID]
            RedisUtils.delEventTask(task_id)
            RedisUtils.removeEventDownloadLock(tenant_id)
